package com.droidblackbox.record;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class RecorderService extends Service {

	private static final String TAG = "RecorderService";
	private boolean mRecordingStatus;
	private MediaRecorder mMediaRecorder;

	@Override
	public void onCreate() {
		mRecordingStatus = false;

		super.onCreate();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		if (mRecordingStatus == false)
			startRecording();

		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		stopRecording();
		mRecordingStatus = false;

		super.onDestroy();
	}

	public boolean startRecording() {
		try {
			Toast.makeText(getBaseContext(), "Recording Started",
					Toast.LENGTH_SHORT).show();

			mMediaRecorder = RecordView.mediaRecorder;

			mRecordingStatus = true;

			return true;
		} catch (IllegalStateException e) {
			Log.d(TAG, e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

	public void stopRecording() {
		Toast.makeText(getBaseContext(), "Recording Stopped",
				Toast.LENGTH_SHORT).show();
		RecordView.mediaRecorder = mMediaRecorder;
	}

}
